﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Domain;
using MyVoteMVC.ExcelObject;
using System.IO;
using System.Drawing;

namespace MyVoteMVC.BackstageManagement.Extensions
{
    public class ProjectModelConverter : IModelConverter<VT_Projects, Project>
    {
        public Project ConvertToModel(VT_Projects entity)
        {
            throw new NotImplementedException();
        }

        public void ReverseConvert(Project model, VT_Projects entityToUpdated)
        {

            throw new NotImplementedException();
        }

        public VT_Projects ConvertToEntity(Project model)
        {
            VT_Projects entityToUpdated = new VT_Projects();
            entityToUpdated.Project_ID = Guid.NewGuid().ToString();

            if (!string.IsNullOrWhiteSpace(model.Project_Address) && Encoding.Default.GetByteCount(model.Project_Address) > 2000)
                throw new ArgumentOutOfRangeException(model.ToString() + "，地址数据超出范围");
            entityToUpdated.Project_Address = model.Project_Address;

            if (!string.IsNullOrWhiteSpace(model.Project_Name) && Encoding.Default.GetByteCount(model.Project_Name) > 300)
                throw new ArgumentOutOfRangeException(model.ToString() + "，名称数据超出范围");
            entityToUpdated.Project_Name = model.Project_Name;

            if (!string.IsNullOrWhiteSpace(model.Project_Type) && Encoding.Default.GetByteCount(model.Project_Type) > 50)
                throw new ArgumentOutOfRangeException(model.ToString() + "，类型数据超出范围");
            entityToUpdated.Project_Type = model.Project_Type;

            if (!string.IsNullOrWhiteSpace(model.Project_Year) && Encoding.Default.GetByteCount(model.Project_Year) > 12)
                throw new ArgumentOutOfRangeException(model.ToString() + "，年份数据超出范围");
            entityToUpdated.Project_Year = model.Project_Year;

            if (!string.IsNullOrWhiteSpace(model.Project_Awards) && Encoding.Default.GetByteCount(model.Project_Awards) > 2000)
                throw new ArgumentOutOfRangeException(model.ToString() + "，获奖数据超出范围");
            entityToUpdated.Project_Awards = model.Project_Awards;

            if (!string.IsNullOrWhiteSpace(model.Project_Description) && Encoding.Default.GetByteCount(model.Project_Description) > 2000)
                throw new ArgumentOutOfRangeException(model.ToString() + "，描述数据超出范围");
            entityToUpdated.Project_Description = model.Project_Description;

            entityToUpdated.ProjectImage = new VT_ProjectImage()
            {
                Project_ID = entityToUpdated.Project_ID,
                Project_Image = model.Project_Image,
                Project_Thumbnail = DomainExtension.Thumbnail(model.Project_Image)
                
            };
            return entityToUpdated;
        }

        
    }

   

    public static class DomainExtension
    {
        public static IList<TEntity> ToNewEntityList<TEntity, TModel>(this IEnumerable<TModel> modelList, IModelConverter<TEntity, TModel> converter)
            where TEntity : class
            where TModel : class
        {
            if (converter == null)
            {
                throw new ArgumentNullException("必须传入一个实现了IModelConverter的实例");
            }

            IList<TEntity> entityList = new List<TEntity>();
            foreach (TModel model in modelList)
            {
                entityList.Add(converter.ConvertToEntity(model));
            }

            return entityList;
        }

        public static byte[] Thumbnail(byte[] bigImage)
        {
            //TODO:Add security checks

            if (bigImage != null)
            {
                bool isBroken = false;
                try
                {
                    using (var bmpstream = new MemoryStream(bigImage))
                    {
                        Bitmap bmp = new Bitmap(bmpstream);
                        if (bmp == null)
                        {
                            //图片损坏
                            isBroken = true;
                        }
                    }
                }
                catch //(Exception ex)
                {
                    //图片损坏
                    isBroken = true;
                }

                if (isBroken)
                    return null;

                var desiredSize = new ImageSize { Width = 128, Height = 128 };
                string contentType = "image/png";
                var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                using (var stream = new MemoryStream(bigImage))
                {
                    return thumbnailCreator.Create(stream, desiredSize, contentType);
                }

            }

            return null;
        }
    }
}
